package utils.bars;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.JTree;
import utils.code.CodeUtils;
import utils.file.FileUtils;
import utils.system.SystemUtils;
import utils.tabbedpane.TabbedPaneUtils;
import utils.text.TextUtils;
import utils.workspace.WorkspaceSelectorUtils;


/**
 *
 * @author Juan Carlos García
 */
public class MenuBarUtils {
    
    
    /**
     * This method creates and customize the MenuBar of our System.
     * 
     * @param object JFrame where the system works
     * @return JMenuBar 
     */
    public static JMenuBar getMenuBar(final JFrame object,final JTabbedPane tabbedPane,final String directory,final JTree fileTree){
  
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu toolsMenu = new JMenu("Tools");
        JMenu dictionariesMenu = new JMenu("Dictionaries");
        JMenu windowMenu = new JMenu("Window");
        JMenu helpMenu = new JMenu("Help");
        
        try{
            JMenuItem newOption = new JMenuItem("New",new ImageIcon(ImageIO.read(MenuBarUtils.class.getResource("/resources/img/new.png")).getScaledInstance(16, 16, 16)));
            JMenuItem newProjectOption = new JMenuItem("New Project", new ImageIcon(ImageIO.read(MenuBarUtils.class.getResource("/resources/img/newProject.png")).getScaledInstance(16, 16, 16)));
            JMenuItem openOption = new JMenuItem("Open",new ImageIcon(ImageIO.read(MenuBarUtils.class.getResource("/resources/img/open.png")).getScaledInstance(16, 16, 16)));
            JMenuItem saveOption = new JMenuItem("Save", new ImageIcon(ImageIO.read(MenuBarUtils.class.getResource("/resources/img/save.png")).getScaledInstance(16, 16, 16)));
            JMenuItem closeOption = new JMenuItem("Close",new ImageIcon(ImageIO.read(MenuBarUtils.class.getResource("/resources/img/close.png")).getScaledInstance(13, 13, 13)));
            JMenuItem changeWorkspace = new JMenuItem("Switch Workspace...",new ImageIcon(ImageIO.read(MenuBarUtils.class.getResource("/resources/img/changeWorkspace.png")).getScaledInstance(16, 16, 16)));
            
            JMenuItem aboutOption = new JMenuItem("About...");
            
            menuBar.add(fileMenu);
            menuBar.add(editMenu);
            menuBar.add(toolsMenu);
            menuBar.add(dictionariesMenu);
            menuBar.add(windowMenu);
            menuBar.add(helpMenu);
            fileMenu.add(newOption);
            fileMenu.add(newProjectOption);
            fileMenu.add(openOption);
            fileMenu.add(saveOption);
            fileMenu.addSeparator();
            fileMenu.add(changeWorkspace);
            fileMenu.addSeparator();
            fileMenu.add(closeOption);
            
            helpMenu.add(aboutOption);
            
        /**
         * OpenOption ActionListener
         */    
        openOption.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               
                String route = WorkspaceSelectorUtils.selectAndReturnWorkspace(JFileChooser.FILES_ONLY);
                File file = new File(route);
                TabbedPaneUtils.addTab(tabbedPane,file.getName());
                
                JTextPane textArea = (JTextPane) tabbedPane.getComponentAt(tabbedPane.getSelectedIndex());
            
                String code = CodeUtils.replaceCodeOnOpen(file);
            
                textArea.setText(code);
               
            }
        });
            
        /**
         * NewOption ActionListener
         */    
        newOption.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                String title = JOptionPane.showInputDialog(null,"Insert the new Class name:");
                
                if(!title.equals("")){
                    
                    TabbedPaneUtils.addTab(tabbedPane,title+".java");
                    tabbedPane.setSelectedIndex(tabbedPane.getTabCount()-1);
                    tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(),title+".java");
                    String codeNewClass = SystemUtils.createNewClass(title);
                
                    JTextPane textArea = (JTextPane) tabbedPane.getComponentAt(tabbedPane.getSelectedIndex());
                
                    textArea.setText(codeNewClass);
                    
                }
            }
        });
            
            /**
            * ChangeWorkspace MenuItem actionListener
            */
           changeWorkspace.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e) { 

                      WorkspaceSelectorUtils.select();

                      object.dispose();
                  }


           });
           /**
            * CloseOption MenuItem actionListener
            */
           closeOption.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

                   System.exit(0);

               }

           });
           
       /**
         * SaveButton ActionListener
         */
          saveOption.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JTextPane textArea = (JTextPane) tabbedPane.getComponentAt(tabbedPane.getSelectedIndex());
                String title = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
                String code = CodeUtils.replaceCodeOnSave(textArea.getText());
                FileUtils.saveFile(directory,title,code,fileTree);
                
            }
        });
        
        }catch(Exception e2){
            
            JOptionPane.showMessageDialog(null,"Error loading image icons\n"+e2.getMessage());
            
        }

        return menuBar;
        
    }
    
}
